//
//  SketchpadView.m
//  公考神器 - 画板
//
//  Created by 毕武侠 on 16/4/25.
//  Copyright © 2016年 毕武侠. All rights reserved.
//

#import "SketchpadView.h"
#import "BHBMyDrawer.h"
#import "BHBDrawer.h"
#import "BHBScrollView.h"

@interface SketchpadView ()

@property (nonatomic, strong) UIButton *backBtn;
@property (nonatomic, strong) UIButton *clearBtn;
@property (nonatomic, strong) UIButton *nextBtn;
@property (nonatomic, strong) UIButton *lastBtn;

@property (nonatomic, strong) BHBMyDrawer   *myDrawer;
@property (nonatomic, strong) BHBDrawer     *drawer;
@property (nonatomic, strong) BHBScrollView *boardView;


@property (nonatomic, strong)NSIndexPath *index;
@property (nonatomic, assign)NSInteger num;
@property (nonatomic, strong)NSArray * linesInfo;
@property (nonatomic, strong)NSArray * canceledLinesInfo;

@end

@implementation SketchpadView

- (instancetype)initWithFrame:(CGRect)frame {
    self = [super initWithFrame:frame];
    if (self) {
        [self initConfigureView];
    }
    return self;
}

- (void)initConfigureView {
    CGFloat width = self.frame.size.width;
    
    UIView *headView = [[UIView alloc] initWithFrame:CGRectMake(0, 0, self.frame.size.width, 40)];
    headView.backgroundColor = MainThemeColor;
    [self addSubview:headView];
    
    self.backBtn = [[UIButton alloc] initWithFrame:CGRectMake(8, 4, 32, 32)];
    self.backBtn.tag = 10;
    [self.backBtn setImage:[UIImage imageNamed:@"palette_back"] forState:UIControlStateNormal];
    [self.backBtn addTarget:self action:@selector(clickAction:) forControlEvents:UIControlEventTouchUpInside];
    [headView addSubview:self.backBtn];
    
    self.nextBtn = [[UIButton alloc] initWithFrame:CGRectMake(width - 60, 0, 40, 40)];
    self.nextBtn.tag = 11;
    [self.nextBtn setImage:[UIImage imageNamed:@"palette_next"] forState:UIControlStateNormal];
    [self.nextBtn addTarget:self action:@selector(clickAction:) forControlEvents:UIControlEventTouchUpInside];
    [headView addSubview:self.nextBtn];
    
    self.lastBtn = [[UIButton alloc] initWithFrame:CGRectMake(width - 100, 0, 40, 40)];
    self.lastBtn.tag = 12;
    [self.lastBtn setImage:[UIImage imageNamed:@"palette_last"] forState:UIControlStateNormal];
    [self.lastBtn addTarget:self action:@selector(clickAction:) forControlEvents:UIControlEventTouchUpInside];
    [headView addSubview:self.lastBtn];
    
    self.clearBtn = [[UIButton alloc] initWithFrame:CGRectMake(width - 140, 0, 40, 40)];
    self.clearBtn.tag = 13;
    [self.clearBtn setImage:[UIImage imageNamed:@"palette_delete"] forState:UIControlStateNormal];
    [self.clearBtn addTarget:self action:@selector(clickAction:) forControlEvents:UIControlEventTouchUpInside];
    [headView addSubview:self.clearBtn];
    
//    [self.myDrawer addObserver:self forKeyPath:@"lines" options:NSKeyValueObservingOptionNew|NSKeyValueObservingOptionOld context:nil];
//    [self.myDrawer addObserver:self forKeyPath:@"canceledLines" options:NSKeyValueObservingOptionNew|NSKeyValueObservingOptionOld context:nil];
    
    //画板view
    self.boardView = [[BHBScrollView alloc] initWithFrame:CGRectMake(0, CGRectGetMaxY(headView.frame), self.frame.size.width, self.frame.size.height - CGRectGetMaxY(headView.frame))];
    
    self.boardView.layer.backgroundColor = [[UIColor blackColor] colorWithAlphaComponent:.25].CGColor;
    [self.boardView setUserInteractionEnabled:YES];
    [self.boardView setScrollEnabled:YES];
    [self.boardView setMultipleTouchEnabled:YES];
    [self.boardView addSubview:self.drawer];
    [self.boardView setContentSize:self.drawer.frame.size];
    [self.boardView setDelaysContentTouches:NO];
    [self.boardView setCanCancelContentTouches:NO];
    [self insertSubview:self.boardView belowSubview:headView];
    
    @weakify(self)
    [RACObserve(self.drawer, isFirst)  subscribeNext:^(NSNumber *value) {
        @strongify(self)
        self.lastBtn.enabled = ![value boolValue];
    }];

    [RACObserve(self.drawer, isLast)  subscribeNext:^(NSNumber *value) {
        @strongify(self)
        self.nextBtn.enabled = ![value boolValue];
    }];
}

- (BHBMyDrawer *)myDrawer {
    if (_myDrawer == nil) {
        _myDrawer = [[BHBMyDrawer alloc] initWithFrame:CGRectMake(0, 0, self.frame.size.width, self.frame.size.height*2)];
        _myDrawer.layer.backgroundColor = [UIColor clearColor].CGColor;
    }
    return _myDrawer;
}

- (BHBDrawer *)drawer {
    if (!_drawer) {
        _drawer = [[BHBDrawer alloc] initWithFrame:CGRectMake(0, 0, self.frame.size.width, self.frame.size.height*2)];
        _drawer.layer.backgroundColor = [UIColor clearColor].CGColor;
        
    }
    return _drawer;
}

- (void)setModel:(PaletteModel *)model {
    self.drawer.model = model;
}

- (void)show {
//    _myDrawer.lines = [NSMutableArray arrayWithArray:self.linesInfo];
//    for (CALayer * layer in _myDrawer.lines) {
//        [_myDrawer.layer addSublayer:layer];
//    }
}

/**
 *  按钮点击事件
 *  @param btn 按钮
 */
- (void)clickAction:(UIButton *)btn {
    switch (btn.tag) {
        case 10:                //back
        {
            if (self.backBlock) {
                self.backBlock();
            }
//            [self.myDrawer.canceledLines removeAllObjects];
//            [self.myDrawer.lines removeAllObjects];
//            [self.myDrawer removeObserver:self forKeyPath:@"canceledLines"];
//            [self.myDrawer removeObserver:self forKeyPath:@"lines"];
        }
            break;
        case 11:                //next
        {
            [self.drawer next];
        }
            break;
        case 12:                //last
        {
            [self.drawer last];
        }
            break;
        case 13:                //delete
        {
            [self.drawer clear];
        }
            break;
            
        default:
            break;
    }
}

- (void)observeValueForKeyPath:(NSString *)keyPath
                      ofObject:(id)object
                        change:(NSDictionary<NSString *,id> *)change
                       context:(void *)context
{
    if([keyPath isEqualToString:@"lines"]){
        NSMutableArray * lines = [_myDrawer mutableArrayValueForKey:@"lines"];
        if (lines.count) {
            [self.clearBtn setEnabled:YES];
            [self.lastBtn setEnabled:YES];
        }else{
            [self.clearBtn setEnabled:NO];
            [self.lastBtn setEnabled:NO];
        }
    } else if([keyPath isEqualToString:@"canceledLines"]){
        NSMutableArray * canceledLines = [_myDrawer mutableArrayValueForKey:@"canceledLines"];
        [self.nextBtn setEnabled:canceledLines.count != 0 ? YES : NO];
    }
}

#pragma mark - ViewSignalProtocol
- (RACSignal *)getSignalWithDictionary:(NSDictionary *)dict {
    NSString *str = dict[@"action"];
    if (!str)   return [RACSignal empty];
    
    return [RACSignal empty];
}

@end
